﻿using OneSoft.DataAccess;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BySoft.Server.DataAccess.UserCode
{
    public partial class BsScheduleMachineDataAccess
    {
        public static DataTable GetBsSchedulemachineByScheduleProcessID(string ScheduleProcessID)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    #region SQL Server
                    DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                    string strcmd = @"SELECT * FROM BS_ScheduleMachine where ScheduleProcessID=@ScheduleProcessID";
                    dpc.AddWithValue("@ScheduleProcessID", ScheduleProcessID);
                    return DataAccessCommon.Select(strcmd, dpc).Tables[0];
                    #endregion
                }
                catch
                {
                    throw;
                }
            }
        }

        public static DataTable GetDTByCondition(string BillDate, string MachineID, string ScheduleMachineID, string ScheduleProcessID, string ScheduleID)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    #region SQL Server
                    DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                    string strcmd = @"select sd.ScheduleID,sm.* from BS_ScheduleProcess sp
                                        left join BS_ScheduleMachine sm on sm.ScheduleProcessID=sp.ScheduleProcessID 
                                        left join BS_ScheduleDetail sd on sd.ScheduleDetailID=sp.ScheduleDetailID
                                        where 1=1";
                    if (!string.IsNullOrEmpty(BillDate))
                    {
                        strcmd += " and CONVERT(varchar(10),sm.BillDate,23)=CONVERT(varchar(10),@BillDate,23)";
                        dpc.AddWithValue("@BillDate", BillDate);
                    }
                    if (!string.IsNullOrEmpty(MachineID))
                    {
                        strcmd += " and sm.MachineID = @MachineID";
                        dpc.AddWithValue("@MachineID", MachineID);
                    }
                    if (!string.IsNullOrEmpty(ScheduleMachineID))
                    {
                        strcmd += " and sm.ScheduleMachineID <> @ScheduleMachineID";
                        dpc.AddWithValue("@ScheduleMachineID", ScheduleMachineID);
                    }
                    if (!string.IsNullOrEmpty(ScheduleProcessID))
                    {
                        strcmd += " and sm.ScheduleProcessID <> @ScheduleProcessID";
                        dpc.AddWithValue("@ScheduleProcessID", ScheduleProcessID);
                    }
                    if (!string.IsNullOrEmpty(ScheduleID))
                    {
                        strcmd += " and sd.ScheduleID <> @ScheduleID";
                        dpc.AddWithValue("@ScheduleID", ScheduleID);
                    }

                    return DataAccessCommon.Select(strcmd, dpc).Tables[0];
                    #endregion
                }
                catch
                {
                    throw;
                }
            }
        }

        public static DataTable GetMachineByScheduleID(string strCode)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    #region SQL Server
                    DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                    string strcmd = @"select sp.ScheduleProcessID from BS_ScheduleDetail s
                                        left join BS_ScheduleProcess sp on sp.ScheduleDetailID=s.ScheduleDetailID
                                        left join BS_ScheduleMachine sm on sm.ScheduleProcessID=sp.ScheduleProcessID
                                        where s.ScheduleID=@ScheduleID";
                    dpc.AddWithValue("@ScheduleID", strCode);
                    return DataAccessCommon.Select(strcmd, dpc).Tables[0];
                    #endregion
                }
                catch
                {
                    throw;
                }
            }
        }
    }
}
